package controller;

import javax.swing.Timer;

import model.dao.AccountDAO;

import client.TokenClient;

import gui.TokenGetter;

public class TokenGetterController {
	private TokenGetter gui;
	private Timer timer;
	private TokenClient client;

	public TokenGetterController(TokenGetter gui) {
		AccountDAO dao = new AccountDAO();
		client = new TokenClient("localhost", 7764);
		this.gui = gui;

		String accountName = (String) gui.getCb_user().getSelectedItem();
		int pin;
		gui.getTxt_pin().setText(
				(pin = dao.getAccount(accountName).getPin()) + "");

		client.setUsername(accountName);
		client.setPin(pin);

		gui.getTxt_token().setText(client.getToken() + "");
		gui.getPb_countDown().setValue(60);

		setAction();
	}

	private void setAction() {
		gui.getBtn_applyChange()
				.addActionListener(new GenNewToken(gui, client));
		gui.getCb_user().addActionListener(new CbbUserAction(gui, client));
		timer = new Timer(1000, new TimerAction(gui, client));
		timer.start();
	}
}
